//
//  HMBottomView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/8.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

class HMBottomView: UIView {
    
    var statusViewModel:HMCellViewModel? {
        
        // 赋值
        didSet {
            retweetButton.setTitle(statusViewModel?.repostsCountStr, forState: .Normal)
            commentButton.setTitle(statusViewModel?.commentsCountStr, forState: .Normal)
            unlikeButton.setTitle(statusViewModel?.attitudesCountStr, forState: .Normal)
 
        }
        
    }
    
    
    private var retweetButton: UIButton!
    private var commentButton: UIButton!
    private var unlikeButton: UIButton!
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        // 添加三个按钮
        retweetButton = setButton("timeline_icon_retweet", title: "转发")
        commentButton = setButton("timeline_icon_comment", title: "评论")
        unlikeButton = setButton("timeline_icon_unlike", title: "赞")
        
        // 添加两条分割线
        let sp1 = addSpliteView()
        let sp2 = addSpliteView()
        
        // 添加约束
        retweetButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        
        commentButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(retweetButton)
            make.leading.equalTo(retweetButton.snp_trailing)
            make.width.equalTo(unlikeButton)
        }
        
        unlikeButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(retweetButton)
            make.leading.equalTo(commentButton.snp_trailing)
            make.trailing.equalTo(self)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            // 约束垂直居中
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }

    }
    
    
    // MARK: -懒加载
    private func setButton(imgName:String,title:String)-> UIButton {
        
        let button = UIButton()
        // 设置title和图标
        button.setTitle(title, forState: UIControlState.Normal)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        // 设置文字大小和颜色
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        // 设置不同状态下的背景图
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        self.addSubview(button)
        return button


}
    
    // 添加分割线
    private func addSpliteView() -> UIView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }



}
